package com.mindbook.vip.gawxchatinfo.controller;

import com.alibaba.fastjson.JSON;
import com.mindbook.vip.gawxchatinfo.entity.user.UserBack;
import com.mindbook.vip.gawxchatinfo.service.user.UserBackService;
import com.mindbook.vip.gawxchatinfo.utils.result.DataResult;
import com.mindbook.vip.gawxchatinfo.utils.result.Result;
import com.mindbook.vip.gawxchatinfo.utils.result.ResultEnum;
import com.mindbook.vip.gawxchatinfo.utils.result.ResultUtils;
import com.mindbook.vip.gawxchatinfo.utils.service.RedisService;
import com.mindbook.vip.gawxchatinfo.utils.wx.AccessTokenUtils;
import com.mindbook.vip.gawxchatinfo.utils.wx.PhoneNumberUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.net.HttpCookie;

@RestController
@RequestMapping("test")
@Api(tags = "测试类，测试集成功能的使用")
public class TestController {

    @Autowired private AccessTokenUtils accessTokenUtils;

    @Autowired
    private UserBackService userBackService;



    @Autowired private RedisService redisService;

    @GetMapping("say")
    @CrossOrigin
    @ApiOperation(value = "测试接口能否正常访问")
    private Result sayHello(){
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("测试成功");
        return result;
    }

    @CrossOrigin
    @PostMapping("say/data")
//    @RequiresPermissions("say:add")
    private DataResult dataResult(HttpServletRequest request){
//        Cookie[] cookies = request.getCookies();
//        System.out.println(JSON.toJSONString(cookies));
        System.out.println("进入了SayData的请求中");
        DataResult<String> dataResult = new DataResult<String>();
        dataResult.setCode(ResultEnum.SUCCESS.getCode());
        dataResult.setMsg("测试成功");
        dataResult.setResult("这样也可以");
        return dataResult;
    }

    /**
     * 测试获取微信小程序的access_token
     * @return
     */
    @GetMapping("accessToken")
    private String getAccessToken(){
        String accessToken = accessTokenUtils.getAccessToken();
        return  accessToken;
    }




    @GetMapping("getKey")
    private String hasKey(){
        Object key = redisService.get("key");
        return key.toString();
    }

    @GetMapping("setKey")
    private boolean setKey(){
        boolean set = redisService.set("key", "test");
        return set;
    }

    @GetMapping("/user")
    public Result getUsername(){
        UserBack userBackList = userBackService.getUserBackList();
        return ResultUtils.genSuccessResult(userBackList);
    }

}
